<?php
class Admin_ProductsController extends App_ControllerAdmin{
	public function init(){
		$this->_helper->layout->setLayout('admin');
		$this->_sess = new Zend_Session_Namespace();
   		if(!@$this->_sess->session){
			$this->_helper->redirector('index','login','admin');
		}
	}
	
	public function indexAction(){
		$mode = new Admin_Model_Products;
		$mode = $mode->show_products();
		$this->view->show_products = $mode;
	}
	
	public function viewAction(){
		$mode = new Admin_Model_Products;	
		$mode=$mode->show_products();
		$this->view->show_products = $mode;
		
		
	}
	
	public function addAction(){
			$mode = new Admin_Model_Catalogs;
			$mode = $mode->show_catalogs();
			$this->view->show_catalogs = $mode;
			
		if($this->_request->isPost()){
			$Request = $this->getRequest()->getPost();
			$mode = new Admin_Model_Products;
			$upload = new Zend_File_Transfer_Adapter_Http();
         	$info = $upload->getFileInfo();
			//print_r($info);die;
         	$picture = $info['image'];
         	$upload->setDestination('public/images/products/');
         	$upload->receive($picture['name']);
			$image['image']=$picture['name'];
			
			//$check=$mode->check_user($Request['email']);
			
			
			if($image['image']!= NULL){
				$data = array(
					'prName' 		=> $Request['name'],
					'prDescribe'	=> $Request['describe'],
					'prCaID' 		=> $Request['category'],
					'prImage'		=> $image['image'],
				);
			}
			else{
				$data = array(
					'prName' 		=> $Request['name'],
					'prDescribe'	=> $Request['describe'],
					'prCaID' 		=> $Request['category'],
				);
			}
			
				$ok = $mode->insert_product($data);
				$this->view->message = 'Thêm Dữ Liệu Thành Công !!!';
			
				
		}
		
	}
	
	public function deleteAction(){
		$mode = new Admin_Model_Products;
		$id = $this->getRequest()->getParam('id');
		if($id){
			$ok = $mode->delete_product($id);
				$this->view->message = 'Xóa Dữ Liệu Thành Công !!!';
		}
		$this->_helper->redirector('view');
	}
	
	public function editAction(){
		$mode = new Admin_Model_Catalogs;
		$mode = $mode->show_catalogs();
		$this->view->show_catalogs = $mode;
		
		$id = $this->getRequest()->getParam('id');
		$mode = new Admin_Model_Products;
		if($this->_request->isPost()){
      		$upload = new Zend_File_Transfer_Adapter_Http();
         	$info = $upload->getFileInfo();
         	$picture = $info['image'];
         	$upload->setDestination('public/images/products/');
         	$upload->receive($picture['name']);
			$image['image']=$picture['name'];
			$Request = $this->getRequest()->getPost();
			if($image['image']!= NULL){
				$data = array(
						'prName' 		=> $Request['name'],
						'prDescribe'	=> $Request['describe'],
						'prCaID' 		=> $Request['category'],
						'prState'		=> $Request['state'],
						'prImage'		=> $image['image'],
				);
			}
			else{
				$data = array(
						'prName' 		=> $Request['name'],
						'prDescribe'	=> $Request['describe'],
						'prCaID' 		=> $Request['category'],
						'prState'		=> $Request['state'],
				);
			}
			$ok = $mode->update_product($id,$data);
			if($ok==NULL){
				$this->view->message = 'Cập Nhật Dữ Liệu Thành Công !!!';
			}
			else{
				$this->view->error = 'Cập Nhật Dữ Liệu Không Thành Công !!!';
			}
		}
			
		if($id){
			$row = $mode->show_product_where($id);
		}
		$this->view->edit_product = $row;
	}
	
	public function lockAction(){
		$mode = new Admin_Model_Products;
		$id = $this->getRequest()->getParam('id');
		if($id){
			$data = array(
					'prState'	=> 1,
			);
			$mode->lock($id,$data);
			$this->view->message = 'Xóa Dữ Liệu Thành Công !!!';
		}
		$this->_helper->redirector('view');
	}
}